/* vim: set ts=2 et sw=2 cindent fo=qroca: */

package com.globant.google.mendoza.command;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.globant.google.mendoza.malbec.Decoder;

import com.globant.google.mendoza.MendozaRequest;
import com.globant.google.mendoza.MendozaServer;
import com.globant.google.mendoza.MendozaServerState;

/** Represents the post encoded cart mendoza server command.
 */
public final class MendozaCheckoutEncodedCommand extends
MendozaBaseCommand {

  /** The class logger.
   */
  private static Log log = LogFactory.getLog(
      MendozaCheckoutEncodedCommand.class);

  /** The base64 cart label. */
  private final String base64Label = "Base64 Cart";

  /** The signature label. */
  private final String signatureLabel = "Signature";

  /** The plain cart label. */
  private final String cartLabel = "Cart";

  /** Creates an instance of MendozaCheckoutEncodedCommand.
   *
   * @param mendozaRequest The mendoza server request.
   *
   * @param mendoza The mendoza server.
   *
   * @param mendozaState The mendoza server state.
   */
  public MendozaCheckoutEncodedCommand(
      final MendozaRequest mendozaRequest, final MendozaServer mendoza,
      final MendozaServerState mendozaState) {
      super(mendozaRequest, mendoza, mendozaState);
      setName("Post encoded cart");
  }

  /** Executes the mendoza server post encoded cart command.
   */
  public void execute() {
    log.trace("Entering execute");
    MendozaCommandResult result = new MendozaCommandResult();
    String commandResultMsg =
      "OK - Post encoded cart command sent to the mendoza server.";
    try {
      setRequestParameters();
      result.setSuccess(commandResultMsg);
    } catch (RuntimeException e) {
      commandResultMsg = e.getMessage();
      result.setError(commandResultMsg);
    }
    setResult(result);
    logCommand(this);
    log.trace("Leaving execute");
  }

  /** Sets the post cart command parameters.
   */
  private void setRequestParameters() {
    log.trace("Entering setRequestParameters");
    String base64Cart =
      getMendozaRequest().getRequestParameters().getProperty("cart");
    if (base64Cart == null) {
      log.trace("Leaving setRequestParameters");
        throw new RuntimeException("The cart cannot be null");
    }
    String signature =
      getMendozaRequest().getRequestParameters().getProperty("signature");
    if (signature == null) {
      log.trace("Leaving setRequestParameters");
      throw new RuntimeException("The signature cannot be null");
    }
    String decodedCart;
    try {
      decodedCart = Decoder.decode(base64Cart);
    } catch (Exception e) {
      log.trace("Leaving setRequestParameters");
      throw new RuntimeException(
          "Mendoza server internal error: Unable to decode cart: \n"
          + base64Cart);
    }
    getMendozaState().setShoppingCart(decodedCart);
    getMendozaState().setCartSignature(signature);
    logRequestParameter(signatureLabel, signature);
    logRequestParameter(base64Label, base64Cart);
    logRequestParameter(cartLabel, decodedCart);
    log.trace("Leaving setRequestParameters");
  }
}

